package com.anuo.app.common.globalhandle;


import com.anuo.app.common.entity.EntityBase;
import com.anuo.app.common.entity.Request;
import com.anuo.app.common.entity.Response;
import com.anuo.app.common.persistence.PageData;
import com.anuo.app.common.persistence.RedisUtils;
import com.anuo.app.common.utils.JsonUtil;
import com.anuo.app.common.utils.StringUtil;
import com.anuo.app.modules.user.service.UserService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class BaseController {
    @Autowired
    UserService userService;

    public BaseController(){

    }

    //region 请求处理
    public <T> T getRequestObject(String encryptString, TypeReference<T> typeReference) {
        //解密请求体
        //encryptString = SecurityUtil.aesDecrypt(encryptString);
        //验证token
        Request req = JsonUtil.fromJson(encryptString, new TypeReference<Request>() {
        });
        checkToken(req.getToken());
        return JsonUtil.fromJson(encryptString, typeReference);
    }

    public <T> T getRequestObjectWithoutToken(String encryptString, TypeReference<T> typeReference) {
        //解密请求体
        //encryptString = SecurityUtil.aesDecrypt(encryptString);
        return JsonUtil.fromJson(encryptString, typeReference);
    }

    public <T> T getRequestObject(String encryptString, Class<T> type) {
        //encryptString = SecurityUtil.aesDecrypt(encryptString);
        return JsonUtil.fromJson(encryptString, type);
    }


    public void checkToken(String token) {
        //checkTokenInRedis(token);
        checkTokenInDB(token);
    }

    public void checkTokenInRedis(String token) {
        //走redis 验证token是否有效
        String redisToken = RedisUtils.getString(token);
        if (StringUtil.isEmpty(redisToken)) {
            throw new AppException("token 失效, 请重新登录", "token_invalid");
        }
    }

    public void checkTokenInDB(String token) {
        //走MySQL 验证token是否有效
        boolean isExistToken = userService.isExistToken(token);
        if (!isExistToken) {
            throw new AppException("token 失效, 请重新登录", "token_invalid");
        }
    }

    //endregion 请求处理

    //region 返回成功响应
    public String rspSuccess() {
        Response response = new Response();
        response.setSuccess(true);
        return JsonUtil.toJson(response);
    }

    public String rspSuccess(String tag) {
        Response response = new Response();
        response.setSuccess(true);
        response.setTag(tag);
        return JsonUtil.toJson(response);
    }

    public <T> String rspSuccess(List<T> list) {
        Response response = new Response();
        response.setSuccess(true);
        response.setEntities(list);

        return JsonUtil.toJson(response);
    }


    public String rspSuccess(EntityBase obj) {
        List<EntityBase> list = new ArrayList<>();
        list.add(obj);
        Response response = new Response();
        response.setSuccess(true);
        response.setEntities(list);
        return JsonUtil.toJson(response);
    }

    public String rspSuccess(PageData pageData) {
        Response response = new Response();
        response.setSuccess(true);
        response.setTotalRow(pageData.getTotalRow());
        response.setEntities(pageData.getEntities());
        return JsonUtil.toJson(response);
    }

    //endregion 返回成功响应

    //region 返回失败响应
    public String rspError(String message) {
        Response response = new Response();
        response.setMessage(message);
        response.setSuccess(false);
        return JsonUtil.toJson(response);
    }

    public String rspError(String message, String errorCode) {
        Response response = new Response();
        response.setErrorCode(errorCode);
        response.setMessage(message);
        response.setSuccess(false);
        return JsonUtil.toJson(response);
    }
    //endregion 返回失败响应



}
